package com.zhiyin.seal.util;


import com.zhiyin.seal.constant.SystemConstants;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Random;

public class StringUtil {


    private static final String ALLCHAR = "0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

    /**
     * 生成6位数字的验证码
     * @return
     */
    public static String generateValidateCode() {
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int r = random.nextInt(10); //每次随机出一个数字（0-9）
            code = code + r;  //把每次随机出的数字拼在一起
        }

        return code;
    }

    /**
     * 返回一个定长的随机字符串(只包含大小写字母、数字)
     *
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
        }
        return sb.toString();
    }

    public static String numberAfterFillZero(String str, int length) {
        StringBuffer buffer = new StringBuffer(str);
        if (buffer.length() >= length) {
            return buffer.toString();
        } else {
            while (buffer.length() < length) {
                buffer.append("0");
            }
        }
        return buffer.toString();
    }

    /**
     * 根据公司id获取申请单据编码，生成规则：公司id+年月日+6位动态码
     * @param companyId 公司id
     * @return 申请单据编码
     */
    public static String GenerateApplyCode(String companyId){
        companyId = numberAfterFillZero(companyId,6);
        String strDate = DateUtil.getDateCodeString();
        String code = generateValidateCode();
        // 拼接字符串
        StringBuffer buffer = new StringBuffer(companyId);
        buffer.append(strDate);
        buffer.append(code);
        return buffer.toString();
    }

    /**
     * 生成盖章序列号
     * @return
     */
    public static String getStampSerialNumber(){
        // 时间
        String strDateTime = DateUtil.CODE_DATETIME_FORMATE.format(new Date());
        // 6位随机字符串
        String code = generateValidateCode();
        // 拼接字符串
        StringBuffer buffer = new StringBuffer(strDateTime);
        buffer.append(code);
        return buffer.toString();
    }

    /**
     * 字节数据转换为16进行字符串
     * @param data
     * @return
     */
    public static String bytes2HexString(byte[]data) {
        String result = "";
        for (int i = 0; i < data.length; i++) {
            result += Integer.toHexString((data[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3);
        }

        return result;
    }

    public static String generateToken(String id,String userName){
        Jwts.builder();//生成
        Jwts.parser();//解密

        JwtBuilder jwtBuilder=Jwts.builder()
                .setId(id)//设置需要加密的内容
                .setSubject(userName)
                .setIssuedAt(new Date())//token保留时间
                .signWith(SignatureAlgorithm.HS256, SystemConstants.WEB_TOKEN_SALT_FIGURE).setExpiration(
                        new Date(new Date().getTime() + SystemConstants.EXPIRE_TIME)
                );

        return jwtBuilder.compact();
    }
}
